const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();
const config = app.siteInfo;

Page({
  data: {
    total_price: 0,
    price: 0,
    cash_price: 0,
    total_cash: 0,
    team_count: 0,
    order_money: 0,
    bg: config.appJson[0].window.navigationBarBackgroundColor,
    classa: (app.tabbarindex === 0 ? false : true) || app.siteInfo.tabbar_type === '2',
    theme: app.themeData,
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    imgUrl: app.imgUrl,
  },
  onLoad(options) {
    let that = this;
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    let opts = options || {};
    // 设置标题
    // let title = options.title;
    // wx.setNavigationBarTitle({
    //   title: title || '推广赚钱',
    // });
    
    this.setData({ 
      opts,
      useTabbar: this.data.useTabbar && app.getTabBarStatus(),
    });
    if (this.data.bg && this.data.bg === '#ffffff' || this.data.bg === '#fff') {
      this.setData({
        color: '#010101',
      });
    } else {
      this.setData({
        color: '#ffffff',
      });
    }
    
  },
  onShow() {
    let that = this;
    let user_info = wx.getStorageSync('user_info');
    if (Number(user_info.is_distributor) !== 1) {
      app.ws_micro_shoprequest({
        url: api.user.index,
        success: function (res) {
          if (res.code === 0) {
            user_info = res.data.user_info;
            if (Number(user_info.is_distributor) === 1) {
              that.setData({
                user_info,
              });
              app.ws_micro_shoprequest({
                url: api.share.get_info,
                success: function (resp) {
                  if (resp.code === 0) {
                    that.setData(res.data);
                  }
                },
              });
            } else {

              app.loadShopSetting().then(res => {
                that.setData({
                  alias: (res && res.alias) || '分销商',
                }, () => {
              wx.showModal({
                title: '您还不是' + that.data.alias + '！',
                content: '请先前往“我的->成为' + that.data.alias + '”处进行申请成为' + that.data.alias,
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    let url = '/ws_micro_shop/pages/index/index';
                    app.util.navigateTo(url);
                  }
                },
              });
                });
              });
              
            }
            wx.setStorageSync('user_info', user_info);
          }
        },
      });
    } else {
      that.setData({
        user_info: user_info,
      });
      app.ws_micro_shoprequest({
        url: api.share.get_info,
        success: function (res) {
          if (res.code === 0) {
            that.setData(res.data);
          }
        },
      });
    }
  },
  collectFormId(e) {
    app.collectFormId(e);
  },
  onNavigate(e) {
    let url = e.currentTarget.dataset.url;
    app.util.navigateTo(url);
  },
  toPayOut(e) {
    app.util.navigateTo('/ws_micro_shop/pages/article-detail/article-detail', { status: 'cashout' }, false);
  },
});
